package com.ruoyi.demo.controller;

import java.util.List;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.demo.domain.AuctionUser;
import com.ruoyi.demo.service.IAuctionUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.demo.utils.TokenUtils;

/**
 * 拍卖小程序用户Controller
 *
 * @author metaee
 * @date 2025-02-24
 */
@Api(tags = "拍卖小程序用户管理")
@RestController
@RequestMapping("/demo/user")
public class AuctionUserController extends BaseController
{
    @Autowired
    private IAuctionUserService auctionUserService;

    /**
     * 查询拍卖小程序用户列表
     */
    @ApiOperation("查询拍卖小程序用户列表")
    @GetMapping("/list")
    public TableDataInfo list(AuctionUser auctionUser)
    {
        startPage();
        List<AuctionUser> list = auctionUserService.selectAuctionUserList(auctionUser);
        return getDataTable(list);
    }

    /**
     * 导出拍卖小程序用户列表
     */
    @ApiOperation("导出拍卖小程序用户列表")
    @Log(title = "拍卖小程序用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AuctionUser auctionUser)
    {
        List<AuctionUser> list = auctionUserService.selectAuctionUserList(auctionUser);
        ExcelUtil<AuctionUser> util = new ExcelUtil<AuctionUser>(AuctionUser.class);
        util.exportExcel(response, list, "拍卖小程序用户数据");
    }

    /**
     * 获取拍卖小程序用户详细信息
     */
    @ApiOperation("获取拍卖小程序用户详细信息")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@ApiParam(value = "拍卖小程序用户ID", required = true) @PathVariable("userId") Long userId)
    {
        return success(auctionUserService.selectAuctionUserByUserId(userId));
    }

    /**
     * 新增拍卖小程序用户
     */
    @ApiOperation("新增拍卖小程序用户")
    @Log(title = "拍卖小程序用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AuctionUser auctionUser)
    {
        return toAjax(auctionUserService.insertAuctionUser(auctionUser));
    }

    /**
     * 修改拍卖小程序用户
     */
    @ApiOperation("修改拍卖小程序用户")
    @Log(title = "拍卖小程序用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AuctionUser auctionUser)
    {
        return toAjax(auctionUserService.updateAuctionUser(auctionUser));
    }

    /**
     * 删除拍卖小程序用户
     */
    @ApiOperation("删除拍卖小程序用户")
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@ApiParam(value = "拍卖小程序用户ID数组", required = true) @PathVariable Long[] userIds)
    {
        return toAjax(auctionUserService.deleteAuctionUserByUserIds(userIds));
    }

    /**
     * 小程序用户登录
     */
    @ApiOperation("小程序用户登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody AuctionUser user) {
        // 验证登录参数
        if (StringUtils.isEmpty(user.getOpenid())) {
            return error("openid不能为空");
        }

        // 根据openid查询用户
        AuctionUser existUser = auctionUserService.selectAuctionUserByOpenid(user.getOpenid());

        if (existUser == null) {
            // 新用户,自动注册
            user.setCreateTime(new Date());
            user.setStatus("0"); // 正常状态
            auctionUserService.insertAuctionUser(user);
            existUser = user;
        }

        // 更新登录信息
        existUser.setLastLoginTime(new Date());
        existUser.setLastLoginIp(IpUtils.getIpAddr());
        auctionUserService.updateAuctionUser(existUser);

        // 生成token
        String token = TokenUtils.generateToken(existUser);

        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        result.put("user", existUser);

        return success(result);
    }
}
